package com.htjc.integral.conversion.service.impl;

import com.htjc.integral.conversion.conf.RedisTem;
import com.htjc.integral.conversion.mapper.EcSystemParamMapper;
import com.htjc.integral.conversion.service.IAppMallScoreCover;
import com.htjc.integral.conversion.service.IEcBpExchangeManagerService;
import com.htjc.integral.conversion.tokenUtil.TokenUtil;
import com.htjc.integral.conversion.util.ResponseUtil;
import com.htjc.integral.model.conversion.EcSystemparam;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.commons.service.util.XssFilterUtil;

import net.sf.json.JSONObject;

@Slf4j(topic = "conversionLogger")
@Service
public class AppMallScoreCover implements IAppMallScoreCover {
	@Autowired
	private IEcBpExchangeManagerService ecBpExchangeManagerServiceImpl;
	
	@Autowired
	private RedisTem redisTem;
	
	@Autowired
	private EcSystemParamMapper ecSystemParamMapper;
	
	@Override
	public String scoreConvertByOne (String token,String jsonpCallback,String jsonStr){
		
		JSONObject result = new JSONObject();
		JSONObject param = JSONObject.fromObject(jsonStr);
		String corporationId = null;
		String orgNo = null;
		String converScore = null;
		
//		String partnerId = TokenUtil.getInstance(redisTem).getUserIdByToken(token);
		String partnerId = "06902692";//先使用固定值测试============aaa====================
		
		if(StringUtils.isBlank(partnerId)){
			result.put("rcode", "1204");
			result.put("resultMsg", "用户未登录");
		}else{
			if(param.containsKey("applicationUserCode")){
				corporationId = param.getString("applicationUserCode");
			}
			if(param.containsKey("orgNo")){
				orgNo = param.getString("orgNo");
			}
			if(param.containsKey("converScore")){
				converScore = param.getString("converScore");
			}
			
			if (!StringUtils.isBlank(corporationId)
					&& !StringUtils.isBlank(orgNo)
					&& !StringUtils.isBlank(converScore)) {
				corporationId = XssFilterUtil.filterString(corporationId);
				converScore = XssFilterUtil.filterString(converScore);
				if(!(Integer.valueOf(converScore) > 0)){
					result.put("rcode", "1");
					result.put("resultMsg", "转换积分不能为0");
					return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
				}
				
				// 转换上限由2W改成20W
//				String uplimit = SgccMallUtil.getInstance().getConfValue("site.integral.exchange.uplimit");
				EcSystemparam ecSystemPara = ecSystemParamMapper.getEcSystemparam("site.integral.exchange.uplimit");
				String uplimit = ecSystemPara.getParamvalue();
				if (StringUtils.isEmpty(uplimit)) {
				    uplimit = "200000";
				}
				log.info("积分转换上限为：" + uplimit);
				if(Integer.valueOf(converScore) > Integer.valueOf(uplimit)){
					result.put("rcode", "1");
					result.put("resultMsg", "单次积分转换上限为" + Integer.valueOf(uplimit));
					return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
				}
				if(!converScore.matches("^[1-9][0-9]{0,4}$")){
					result.put("rcode", "1");
					result.put("resultMsg", "单次积分转换上限为2万");
					return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
				}
				if (converScore.matches("^[1-9][0-9]{0,4}$") && Integer.valueOf(converScore) > 0) {
					orgNo = XssFilterUtil.filterString(orgNo);
					
					log.info("输入积分进行转换results==>【partnerId:{},corporationId:{},orgNo:{},converScore:{}】",partnerId,corporationId,orgNo,converScore);
					String results = this.ecBpExchangeManagerServiceImpl.scoreConvertByOne(partnerId, corporationId, orgNo, converScore);
					log.info("输入积分进行转换results++++++++++++++++++++++++++++"+results);
					
					JSONObject resultJson = JSONObject.fromObject(results);
					log.info("输入积分进行转换resultJson++++++++++++++++++++++++++++++++++"+resultJson);
					if(resultJson.containsKey("flag")&&resultJson.getString("flag").equals("0")){
						result.put("rcode", "0");
						result.put("resultMsg", "调用接口成功");
						result.put("results", results);
					}else{
						result.put("rcode", "1");
						if(resultJson.getString("message")!=null){
							result.put("resultMsg", resultJson.getString("message"));
						}else{
							result.put("resultMsg", "接口调用失败!");
						}
						
						result.put("results", results);
					}

				}
			}else{
				result.put("rcode", "1");
				result.put("resultMsg", "传入参数为空");
			}
		}

		
		return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
	}
}
